﻿@using Microsoft.Web.Mvc
@using SapaFinance.DTO
@using SapaFinance.UI.Controllers
@using Telerik.Web.Mvc.UI
@model IEnumerable<RequestDTO>
@{
    ViewBag.Title = "Список заявок";
}
<h2>
    Список заявок</h2>
<div>
    @(Html.Telerik().Grid(Model)
               .Name("Grid")
               .DataKeys(keys => { keys.Add(o => o.UID); })
               .ToolBar(commands => commands.Insert())
               .Columns(columns =>
                        {
                            columns.Template(@<text>
    <input name="checkedRecords" type="checkbox" value="@item.UID" title="checkedRecords" /></text>).Width(40);
                            columns.Bound(o => o.UID).Hidden(true);
                            columns.Bound(o => o.Number).Width(60);
                            columns.Bound(o => o.PaymentPeriod).Format("{0:dd/MM/yyyy}").Width(100);
                            columns.Bound(o => o.Payer).Width(150);
                            columns.Bound(o => o.Payee).Width(150);
                            columns.Bound(o => o.PaidPercentByOrder).Width(70);
                            //columns.Bound(o => o.Payment).Width(100);
                            //columns.Bound(o => o.CurrencyName).Width(50).Title("");
                            columns.Template(@<text>@item.Payment@item.CurrencyDetail.CurrencyName</text>).Title("Оплатить").Width(150);
                            columns.Bound(o => o.PaymentTypeName).Width(100);
                            columns.Bound(o => o.CreatedBy).Width(200);
                            columns.Bound(o => o.CfoName).Width(100);
                            columns.Bound(o => o.IsAccepted).ClientTemplate("<input type='checkbox' name='isAccepted' value='<#= IsAccepted #>' />").Width(90);
                            columns.Template(r => @Html.ActionLink<HomeController>(x => x.Payments(), "посмотреть", new { id = "{0}" }).ToHtmlString()).Title("Оплаты").Width(100);
                            columns.Template(r => @Html.ActionLink<HomeController>(x => x.Orders(), "посмотреть", new { id = r.UID }).ToHtmlString()).Title("Распоряжения").Width(100);
                            columns.Template(r => @Html.ActionLink<RequestController>(x => x.Clone(r.UID), "клон", new { id = "{0}" }).ToHtmlString()).Title("").Width(50);
                            columns.Command(commands =>
                                            {
                                                commands.Edit();
                                                commands.Delete();
                                            });
                        })
               .DataBinding(dataBinding => dataBinding.Server()
                                               .Select("GetRequests", "Request")
                                               .Insert("Insert", "Request")
                                               .Update("Update", "Request")
                                               .Delete("Delete", "Request")
               )
               .Sortable(sorting => sorting.Enabled(true))
               .Selectable(x => x.Enabled(true))
               .Editable(x => x.Enabled(true)
                                  .Mode(GridEditMode.PopUp)
                                  .Window(p => p.ClientEvents(z => z.OnOpen("CenterPopupWindow")))))
</div>
<p>
    <button class="t-button t-state-default" onclick="displayCheckedRequests()">
        Выбрать заявки</button>
</p>
<div id="actorselectrequests">
</div>
@Html.ValidationSummary(true)
<script type="text/javascript">
    function displayCheckedRequests() {
        var $checkedRecords = $(':checked');

        if ($checkedRecords.length < 1) {
            alert('Ни одна заявка не выбрана');
            return;
        }
        $('#actorselectrequests').load('@Url.Action("DisplayCheckedRequests", "Request")', $checkedRecords);
    }
       
</script>
